package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        memberDao.add(member);
    }


    @Override
    public Map<String, Object> getMemberReport() {
        List<String> months = new ArrayList<>();
        List<Long> memberCount = new ArrayList<>();
        //获取前12个月份
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        for (int i = 0; i < 12; i++) {
           calendar.add(Calendar.MONTH,1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            String date = simpleDateFormat.format(calendar.getTime());
            months.add(date);
        }

        //统计每个月对应的会员数量
        for (String month : months) {
         month += "-31";
         long count = memberDao.findCountBeforeByDate(month);
         memberCount.add(count);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",memberCount);

        return map;
    }

    //获取会员性别占比
    @Override
    public Map<String, Object> getMemberSexReport() {
        //查询MemberSexCount的数据
        List<Map<String,Object>> mapList = memberDao.getMemberSexReport();

        //取出mapList中的name
        List<Object> memberSexNames = new ArrayList<>();
        for (Map<String, Object> map : mapList) {
            Object name = map.get("name");
            memberSexNames.add(name);
        }
        //组合数据
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("memberSexNames",memberSexNames);
        resultMap.put("memberSexCount",mapList);
        return resultMap;
    }

    //获取不同年龄段会员占比
    @Override
    public Map<String, Object> getMemberAgeReport() {
        //查询MemberAgeCount的数据
        List<Map<String,Object>> mapList = memberDao.getMemberAgeReport();

        //取出mapList中的name
        List<Object> memberAgeNames = new ArrayList<>();
        for (Map<String, Object> map : mapList) {
            Object name = map.get("name");
            memberAgeNames.add(name);
        }
        //组合数据
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("memberAgeNames",memberAgeNames);
        resultMap.put("memberAgeCount",mapList);
        return resultMap;
    }
    @Override
    public Map<String, Object> getMemberReportAdd(Date startDate, Date endDate) {
        Map<String,Object> map = new HashMap<>();
        List<String> months = new ArrayList<>();
        List<Long> memberCount = new ArrayList<>();
        //处理从前端传回的数据
        int startYear = startDate.getYear();
        int endYear = endDate.getYear();
        int yearDiffer = endYear - startYear;

        int startMonth = startDate.getMonth();
        int endMonth = endDate.getMonth();
        int monthDiffer = endMonth - startMonth;
        //两个日期相差的总月份
        int allMonthDiffer = yearDiffer * 12 + monthDiffer;
        //Calendar要设置开始循环的值
        int cilculation = 0 - allMonthDiffer;

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endDate);
        calendar.add(Calendar.MONTH,cilculation);

        //获取相差月份
        for (int i = 0; i < allMonthDiffer; i++) {
            calendar.add(Calendar.MONTH,1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            String date = simpleDateFormat.format(calendar.getTime());
            months.add(date);
        }
        //如果用户选择的是同一个月份,则只查询当月的数据

        //统计每个月对应的会员数量
        for (String month : months) {
            String startDay = month + "-1";
            String endDay = month + "-31";
            long memCount = memberDao.findMemberCountByMonth(startDay,endDay);
            memberCount.add(memCount);
        }

        map.put("months",months);
        map.put("memberCount",memberCount);
        return map;
    }
}
